import Vue from "vue";
import VueRouter from "vue-router";
// import store from "utils/localstorage";

Vue.use(VueRouter);

// 创建路由对象
const routes = [

  {
    path: "/",
    redirect: "/specia",
    component: () => import("../layout/Content.vue"),
    children: [
      {
        path: "/login",
        name: "登录",
        meta: {
          selectRouter: '/login' //判断左侧菜单显示状态
        },
        component: () => import("../pages/LoginPage/index.vue"),
      },
      {
        path: "/specia",
        name: "专题创建",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/specia' //判断左侧菜单显示状态
        },
        component: () => import("../pages/SpecialPage/index.vue"),
      },
      {
        path: "/datafilter",
        name: "数据筛选",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/datafilter' //判断左侧菜单显示状态
        },
        component: () => import("../pages/DataFilterList/index.vue"),
      },
      {
        path: "/datafilterdetail/:id",
        name: "sjsxDetail",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/datafilterdetail' //判断左侧菜单显示状态
        },
        component: () => import("../pages/DataFilterDetail/index.vue"),
      },
      {
        path: "/history1",
        name: "我的分析",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/history1' //判断左侧菜单显示状态
        },
        component: () => import("../pages/AnalyzePage/index.vue"),
      },
      {
        path: "/history2",
        name: "热点报告",
        meta: {
          requiresAuth: false, // 设置为 false 表示不需要登录即可访问
          selectRouter: '/history2' //判断左侧菜单显示状态
        },
        component: () => import("../pages/HotPage/index.vue"),
      },
      {
        path: "/userpage",
        name: "个人中心",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/userpage' //判断左侧菜单显示状态
        },
        component: () => import("../pages/UserPage/index.vue"),
      },
      {
        path: "/report",
        name: "报告",
        meta: {
          requiresAuth: true, // 需要登录才能访问
          selectRouter: '/history1' //判断左侧菜单显示状态
        },
        component: () => import("../pages/ReportPage/index.vue"),
      },
      {
        path: "/viewreport",
        name: "查看报告",
        meta: {
          requiresAuth: false, // 设置为 false 表示不需要登录即可访问
          selectRouter: '/history2' //判断左侧菜单显示状态
        },
        component: () => import("../pages/ViewReportPage/index.vue"),
      }
    ],
  },

];


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



const router = new VueRouter({
  // 配置URL和组价直接的映射关系
  routes,
});
// 将router对象传入到vue实例中


router.beforeEach((to, from, next) => {
  let selectRouter = '';
  switch (to.meta.selectRouter) {
    case '/specia':
      selectRouter = '/specia';
      break;
    case '/datafilter':
    case '/datafilterdetail':
      selectRouter = '/datafilter';
      break;
    case '/history1':
      selectRouter = '/history1';
      break;
    case '/history2':
      selectRouter = '/history2';
      break;
    case '/userpage':
      selectRouter = '/userpage';
      break;
    case '/report':
      selectRouter = '/history1';
      break;
    case '/viewreport':
      selectRouter = '/history2';
      break;
    case '/login':
      selectRouter = '';
      break;
    default:
      // 如果没有匹配的路径，则执行默认操作（可选）
      break;
  }
  // 将 selectRouter 设置为 Vue 实例的属性，以便在子组件中访问


  Vue.prototype.$selectRouter = selectRouter;
  next();

  
  // 如果不需要登录或用户已登录，则放行
  // next()
});
export default router;
